#include <iostream>
#include <fstream>

#include "src/Volume.h"
#include "src/MarchingCubes.h"
#include "src/Display.h"

int main(int argc, char **argv) {
    // Compute a scalarfield from a single octave of perlin noise
    auto data = Volume::generate();

    // Compute isosurface using marching cube
    auto mc = new MarchingCubes(data);
    mc->exec();
    auto mesh = mc->get_mesh();
//    auto display = new Display(mc, argc, argv);
//    display->exec();

    // Export the result as an .obj file
    std::ofstream out;
    out.open("test.obj");
    if (!out.is_open())
        return 1;
    out << "g " << "Obj" << std::endl;
    for (size_t i = 0; i < mesh->vertices.size(); i++)
        out << "v " << mesh->vertices.at(i).x << " " << mesh->vertices.at(i).y << " " << mesh->vertices.at(i).z << '\n';
    for (size_t i = 0; i < mesh->vertices.size(); i++)
        out << "vn " << mesh->normals.at(i).x << " " << mesh->normals.at(i).y << " " << mesh->normals.at(i).z << '\n';
    for (size_t i = 0; i < mesh->indices.size(); i += 3) {
        out << "f " << mesh->indices.at(i) + 1 << "//" << mesh->indices.at(i) + 1
            << " " << mesh->indices.at(i + 1) + 1 << "//" << mesh->indices.at(i + 1) + 1
            << " " << mesh->indices.at(i + 2) + 1 << "//" << mesh->indices.at(i + 2) + 1
            << '\n';
    }
    out.close();

    return 0;
}
